package yr.website

import grails.transaction.Transactional

import java.awt.Color
import java.awt.Font
import java.awt.Graphics
import java.awt.image.BufferedImage

@Transactional
class UserService {
    def BufferedImage image
    def String str
    def static char[] code = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789".toCharArray()
    def static final String SESSION_CODE_NAME = "code"
    def static
    final PRIKEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCJu/ox1nAsPC6r0VMO9QtIBEKYjbXvYfczk2AY6Nyb1gpUEDxrF5F6G5WO47flQmB7FERXy0mUc1w/pcgZIxijcBz0F1NGthcSsFKWfDPF0WtAQVGBUtHr8+tvN7/Uhte4xst17JxX82uo65xXIOp3WBbW4Jm0h9bKKhufGWS2nxVYV85mD1HiN+IhaijRc87FBHrmkg1v6SG/vN+p1DGf5SjnYyeMHi/+OXy9rvRU6HEtAW6968eHsgLR/B+IwaDcHVbAgb/Gdyn4pAAju+YZ5Co53eXPM61RtwyEj+1Pn3QaGAmlPp8MFKchuS3qD9z5nUfYBScyoyygh5WfhoztAgMBAAECggEAAq1uza03fQSme5yUGsRUtc+vY7vNf5vN7Gi/A3zzXOwqQzwVkJdispb6r05p8sD5kyrvsS04fvRHJM2aODLgdbzmMzCI4+UCSdC3qkk70GqjxRn/9Z2giw+w0fapef4tD2B98ArrdyI6Q4xg+6ieJjfGKeMv2bySRlfPNaaOuKYTnGwvrBRu/XmvjXa/qZyY3rctBbbJpHGGFWZWjkBvpJ96EbvsXRgNYyqzzT3vaWv4xKzDn+3WLqvTt5kd1A65q6PbHuZKYFv/+Rg32tbwgAEaC0KZBw+HoTEyMdYAV9GhEdgRJW5rDiSx7+iCw9GnbxTZBg6Y23p9Aw8E+E0g2QKBgQDiYeUKy0Wmny5KnmbGMoot2UwmckUdwhFYAk6iLdwBErU13Xbq9MwJJcFrI//eWdsGd21OY3PUjLdvQV5lZHcR9gckGGMyyytZatkhK0rYYahvMX+7ujPDjGa9o1a6DPDE17aeQaeqR+XdJLC5l0MH6Wkb63l0iuVksrRG2qUx/wKBgQCbwQnadWQt5HV99ksYmgmuuRLOcrsuhwcavCPmjw+FIt1skPUO+UwG2g77ggaQrzhhVgYzgShakHpMJSVG95Il7wX4O1O+r0O1z0LX7Qd9p9YLVjRPhp7ropX1DWQqdIWDj1BXZUjklAHDCP/hKu3v13tIrYaxCFMu5vyyK74pEwKBgQC2VIJVH0MJ4Es4bjv4hdwk8U8T6BUi0puFdtY+aRE5DAhHSgftVRzAgVg3IteCDn0JyEaRTCiOmIdlZBDTOQdtv+ogeEPtNRXE/vnCbUvq2zz90bA/Usk+Rma5dkTztFh3MgYzkE2qKB5K7rInyFyEksnFYvJ/mx8G66enKZLDoQKBgCq1pXENLbpT4k/3IKKPYsZRo7GOzns0taMEIkgPxK66X8NsgHZpsApU8zTOc+Rq1+gBAdRPv3QOGCsXKICsVJwoThfClPU99DA/tFGngp1XT59uNQv1m1B8FejLkkL+DrqDqwE8Gd/sIguMRTZGYlaZXQoR41RaNw0uc+6bhhqXAoGAXXBFaHT7MCL//4w15UunRXyAhB7tzC/4qt9R9tdcifQ3IYMuWTGp+fYczBAbqS0UKeq3WetnFk+vgg38ld+oteQv23qnWMKKsDuhH4NNBTTwXCoPlvV/0dtntRXFXJEJcYHZUtyoMnIjzINWLE6rh4bhxuCiVt1I0AnA7FoVnUo="

    def String checkCode(String sessionCode, String code) {
        def str = ""
        if (sessionCode.isEmpty()) {
            str = "1"
        }
        if (code.isEmpty()) {
            str = "1"
        }
        if (sessionCode.equalsIgnoreCase(code)) {
            str = "0"
        } else {
            str = "1"
        }
    }

    def BufferedImage getImage() {
        return this.image
    }

    /*
     * 取得图片的验证码
     */

    def String getStr() {
        return this.str
    }

    def get() {
        // 在内存中创建图象
        int width = 85, height = 20
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
        // 获取图形上下文
        Graphics g = image.getGraphics()
        // 生成随机类
        Random random = new Random()
        // 设定背景色
        g.setColor(getRandColor(200, 250))
        g.fillRect(0, 0, width, height)
        // 设定字体
        g.setFont(new Font("Times New Roman", Font.PLAIN, 18))
        // 随机产生155条干扰线，使图象中的认证码不易被其它程序探测到
        g.setColor(getRandColor(160, 200))
        for (int i = 0; i < 155; i++) {
            int x = random.nextInt(width)
            int y = random.nextInt(height)
            int xl = random.nextInt(12)
            int yl = random.nextInt(12)
            g.drawLine(x, y, x + xl, y + yl)
        }
        // 取随机产生的认证码(4位数字)
        String sRand = ""
        for (int i = 0; i < 4; i++) {
            String rand = String.valueOf(code[random.nextInt(code.length)])
            sRand += rand
            // 将认证码显示到图象中
            g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)))
            // 调用函数出来的颜色相同，可能是因为种子太接近，所以只能直接生成
            g.drawString(rand, 13 * i + 6, 16)
        }
        // 赋值验证码
        this.str = sRand

        // 图象生效
        g.dispose();
        // ByteArrayInputStream input = null;
        // ByteArrayOutputStream output = new ByteArrayOutputStream();
        // try {
        // ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
        // ImageIO.write(image, "JPEG", imageOut);
        // imageOut.close();
        // input = new ByteArrayInputStream(output.toByteArray());
        // } catch (Exception e) {
        // System.out.println("验证码图片产生出现错误：" + e.toString());
        // }
        // this.image = input
        this.image = image
    }

    /*
     * 给定范围获得随机颜色
     */

    def Color getRandColor(int fc, int bc) {
        Random random = new Random()
        if (fc > 255)
            fc = 255
        if (bc > 255)
            bc = 255
        int r = fc + random.nextInt(bc - fc)
        int g = fc + random.nextInt(bc - fc)
        int b = fc + random.nextInt(bc - fc)
        return new Color(r, g, b)
    }

    def Boolean save(User user) {
        def flag = true
        user.recordDate = new Date()
        user.save()
        if (!user.save()) {
            user.errors.allErrors.each {
                println it
            }
            flag = false
        }
        return flag
    }
}
